package com.telsoft.hms.function;

import java.sql.SQLException;

import com.telsoft.hms.data.DATA_Login;
import com.telsoft.hms.entity.ETT_AM_USER;
import com.telsoft.hms.lib.admin.client.data.ClientDataPreprocessor;
import com.telsoft.hms.lib.security.server.WsRuntime;
import com.telsoft.hms.util.StringUtil;

public class HMS_Login extends ClientDataPreprocessor {

	public com.telsoft.hms.entity.ETT_GetDataLoginRT getDataLogin(
			long lCurrentHospitalId, int dcLanguageCode, String sStatus) {
		return new com.telsoft.hms.entity.ETT_GetDataLoginRT();
	}

	public String login(String strUsername, String strPassword,
			int iLanguageCode) throws Exception {
		String strSercurityKey = null;
		try {
			ETT_AM_USER ettAmUser = authenUsernamePass(strUsername, strPassword);

			if (ettAmUser != null) {
				String hospitalKey = com.telsoft.hms.util.StringUtil
						.getHospitalKey(strUsername);
				WsRuntime wsRunTime = new WsRuntime();
				strSercurityKey = wsRunTime.createSession(ettAmUser,
						getMaxSessionLogin(hospitalKey), iLanguageCode,
						hospitalKey);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
		return strSercurityKey;
	}

	public ETT_AM_USER authenUsernamePass(String strUser, String strPass)
			throws SQLException, Exception {
		if (StringUtil.isNullOrEmpty(strUser)) {
			throw new Exception("ERR-0006");
		} else if (StringUtil.isNullOrEmpty(strPass)) {
			throw new Exception("ERR-0007");
		} else if (com.telsoft.hms.util.StringUtil.getHospitalKey(strUser)
				.equals("")) {
			throw new Exception("ERR-0005");
		} else {
			String keyHospital = com.telsoft.hms.util.StringUtil
					.getHospitalKey(strUser.toString());
			DATA_Login dataLogin = new DATA_Login();
			return dataLogin.login(keyHospital, strUser, strPass);
		}
	}
}
